package com.dooioo.pns.business.service;

import com.alibaba.fastjson.JSONArray;
import com.dooioo.pns.base.service.BaseService;
import com.dooioo.pns.entity.model.MessageRead;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.pns.business.service
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-05-14 上午11:01)
 * Description: 消息阅读业务实体
 */
@Service
public class MessageReadService extends BaseService<MessageRead>{

	/**
	 * 根据接收方工号集合，插入消息接收信息
	 * @param messageId 消息id
	 * @param appId 应用app ID
	 * @param userCodes 接收方工号集合
	 * @return boolean
	 */
	public boolean insertReadByUserCodes(int messageId, int appId,List<String> userCodes) {
		Map<String,Object> read = new HashMap<>();
		read.put("messageId",messageId);
		read.put("userCodes",userCodes);
		return super.insert(sqlId("insertReadByUserCodes"),read);
	}

	/**
	 * 根据接受的appId 插入消息接收人
	 * @param messageId 消息id
	 * @param appId 应用appId
	 * @return boolean
	 */
	public boolean insertReadByAppId(int messageId, int appId) {
		Map<String,Object> read = new HashMap<>();
		read.put("messageId",messageId);
		read.put("appId",appId);
		return super.insert(sqlId("insertReadByAppId"),read);
	}

	/**
	 * 跟新消息为已读
	 * @param userCode 工号
	 * @param messageId 消息id
	 * @param status status 状态码
	 * @return
	 */
	public boolean updateStatus(int userCode, int messageId,int status) {
		Map<String,Object> map = new HashMap<>();
		map.put("userCode",userCode);
		map.put("messageId",messageId);
		map.put("status",status);
		return super.update(sqlId("updateStatus"), map);
	}

	/**
	 * 更新消息为已读
	 * @param userCode 用户工号
	 * @param appId appid
	 * @param businessId 业务分类id
	 * @return true 成功，false 失败
	 */
	public boolean updateStatusByAppIdAndBusinessId(int userCode,int appId,int businessId) {
		Map<String,Object> map = new HashMap<>();
		map.put("userCode",userCode);
		map.put("appId",appId);
		map.put("businessId",businessId);
		return super.update(sqlId("updateStatusByAppIdAndBusinessId"),map);
	}

	/**
	 * 更新消息 为已读
	 * @param userCode
	 * @param messageIdArray
	 * @return
	 */
	public boolean readMessage(int userCode, JSONArray messageIdArray) {
		Map<String,Object> map = new HashMap<>();
		map.put("userCode",userCode);
		map.put("messageIdArray",messageIdArray);
		return super.update(sqlId("readMessage"),map);
	}

	/**
	 * 获取工号未读消息的总条数
	 * @param userCode
	 * @return
	 */
	public int getUnreadCount(int userCode,int appId) {
		Map<String,Object> map = new HashMap<>();
		map.put("userCode",userCode);
		map.put("appId",appId);
		return super.count(sqlId("queryUnreadCount"),map);
	}
}
